
var page=1;
var limit=6;

var count;  //数据总数

var orderlist;

var account = $api.getStorage('account');


var type='商品订单';

var status='';


var a=0;
apiready = function(){

    //切换订单类型
    var lnav = $api.domAll(".selli");  
    for(var i in lnav){
        $api.addEvt(lnav[i], 'click', function(){
             
             for (var i = 0; i < lnav.length; i++) {
			     $api.removeCls(lnav[i], 'selactive');
			 }
			 
             $api.addCls(this,'selactive');    
             
           //  var mid=$api.attr(this, '');
             type=$api.text(this).trim();    //类型
             
             $api.text($api.byId('topbartitle'),type);
             $api.css($api.byId('typemenu'),'display:none');
             a=0;
             init(1,6);
             
             
            
        });
        
    }
    
    //切换订单状态
    var statusli = $api.domAll("#header-bottom-ul li");  
    for(var i in statusli){
        $api.addEvt(statusli[i], 'click', function(){
             
             for (var i = 0; i < statusli.length; i++) {
			     $api.removeCls(statusli[i], 'header-bottom-ul-current');
			 }
			 
             $api.addCls(this,'header-bottom-ul-current');  
             status=$api.attr(this,'tapmode');
             
             init(1,6);
             
        
            
        });
        
    }
    
    

 
    //添加订单改变事件
    api.addEventListener({
	    name: 'changeOrder'
	}, function(ret){
	    if(ret){
	        init(page,limit);
	    }
	});
    
    
    init(page,limit);
    
    
    //上拉
	 api.addEventListener({
        name: 'scrolltobottom'
     }, function (ret, err) {
         if(limit<count){
                 limit+=6;
                 init(page,limit);
               
         }else{
               api.toast({
	                msg: '下面没有啦...',
	                duration:1000,
	                location: 'bottom'
	            });
         
         }
        
         
    });
    
 
};



//初始化数据
function init(page,limit){
       
       var param = '?page='+page+'&limit='+limit+'&account='+account+'&type='+type+'&status='+status;
       do_ajax('/app/myorders'+param,'get',{},function(ret,err){
             
			  if(ret){
	            count=ret.count;
	            orderlist=ret.data;  
				var dataHtml = '';
			    if(orderlist.length>0){
			       for(var i=0;i<orderlist.length;i++){
			       
			            if(type=='商品订单'){
					       
						        dataHtml += '<div class="am-u-sm-12" style="margin-top: 5px;padding:0px;font-size:14px;" id=\'order'+i+'\'>'
						        	dataHtml += '<div class="am-u-sm-12" style="background: #FFFFFF;padding:5px 10px;font-size:14px;">';
						        	dataHtml += '<span>'+orderlist[i].orderNo+'</span>';
						        	dataHtml += '<span style="float: right;color:#f40;">'+orderlist[i].status+'</span>';
						        	dataHtml += '</div>';
						        	dataHtml += '<div class="am-u-sm-12" style="background: #FEEBDC;" onclick="orderinfo('+i+')">';
						        	dataHtml += '<div class="am-u-sm-12" style="padding:0px">';
						        	dataHtml += '<div  style="border-bottom:1px solid #eaedf1;padding:0px;">';
							        	dataHtml += '<table  border="0" width="100%" style="margin:8px 0px;table-layout:fixed;">';
							        	dataHtml += '<tr>';
							        	dataHtml += '<td rowspan="3" valign="middle" style="width:70px;">';
							        	dataHtml += '<img src="'+serviceUrl+'/files/'+(orderlist[i].item[0].proDefImg||0)+'"  style="width:65px;height:70px;"/>';
							        	dataHtml += '</td>';
							        	dataHtml += '<td rowspan="2" width="60%"  style="word-break:break-all;font-size:14px"  valign="top">';
							        	dataHtml += orderlist[i].item[0].proName;
							        	dataHtml += '</td>';
							        	dataHtml += '<td valign="top" align="right" >￥'+orderlist[i].item[0].price.toFixed(2)+'</td>';
							        	dataHtml += '</tr>';
							        	dataHtml += '<tr></tr>';
							        	dataHtml += '<td  valign="bottom" align="left" style="font-size:10px;max-width:110px;">X'+orderlist[i].item[0].num+'</tr>';
							        	dataHtml += '</table>';
							       dataHtml += '</div>';
							       dataHtml += '</div>';
							       dataHtml += '</div>';
							       
							       dataHtml += '<div class="am-u-sm-12" style="background: #FFFFFF;text-align: right;border-bottom:1px solid #e5e5e5;padding: 5px 10px">';
							       dataHtml += '共'+orderlist[i].buyNum+'件商品 &nbsp;&nbsp;合计:￥'+orderlist[i].proTotal.toFixed(2)+'';
							       dataHtml += '</div>';
							       dataHtml += '<div class="am-u-sm-12" style="background: #FFFFFF;text-align: right;padding:5px 10px">';
							       dataHtml += '<button type="button" onclick="delorder('+i+')" class="am-btn am-btn-warning">删除</button>&nbsp;&nbsp;';
							       
							       if(orderlist[i].status=='待支付'){
				 			          dataHtml += '<button type="button" class="am-btn am-btn-warning" onclick="pay('+i+')">支付</button>';
							       }else if(orderlist[i].status=='已发货'||orderlist[i].status=='待收货'){
							          dataHtml += '<button type="button" class="am-btn am-btn-warning" onclick="takeback('+i+')">收货</button>';
							       }else if(orderlist[i].status=='交易完成'){
							          dataHtml += '<button type="button" class="am-btn am-btn-warning" onclick="addcomment('+i+')">评价</button>';
							       }else if(orderlist[i].status=='已评价'){
							          dataHtml += '<button type="button" class="am-btn am-btn-warning" onclick="addcomment('+i+')">已评价</button>';
							       }
							       
							       dataHtml += '</div>';
							       dataHtml += '';
					           dataHtml += '</div>';
			            }
			            
			            if(type=='旅游订单'){
			                dataHtml += '<div class="am-u-sm-12" style="margin-top: 5px;padding:0px" id=\'order'+i+'\'>'
						        	dataHtml += '<div class="am-u-sm-12" style="background: #FFFFFF;padding:5px 10px">';
						        	dataHtml += '<span>'+orderlist[i].orderNo+'</span>';
						        	dataHtml += '<span style="float: right;color:#f40">'+orderlist[i].status+'</span>';
						        	dataHtml += '</div>';
						        	dataHtml += '<div class="am-u-sm-12" style="background: #FEEBDC" onclick="orderinfo('+i+')">';
						        	dataHtml += '<div class="am-u-sm-12" style="padding:0px">';
						        	dataHtml += '<div  style="border-bottom:1px solid #eaedf1;padding:0px;">';
							        	dataHtml += '<table  border="0" width="100%" style="margin:8px 0px;table-layout:fixed;">';
							        	dataHtml += '<tr>';
							        	dataHtml += '<td rowspan="3" valign="middle" style="width:70px;">';
							        	dataHtml += '<img src="'+serviceUrl+orderlist[i].proDefImg+'"  style="width:65px;height:70px;"/>';
							        	dataHtml += '</td>';
							        	dataHtml += '<td rowspan="2" colspan="2" width="60%"  style="word-break:break-all;font-size:14px"  valign="top">';
							        	dataHtml += orderlist[i].tourname;
							        	dataHtml += '</td>';
							        	//dataHtml += '<td valign="top" align="right" >￥'+orderlist[i].item[0].price.toFixed(2)+'</td>';
							        	dataHtml += '</tr>';
							        	dataHtml += '<tr></tr>';
							        	dataHtml += '<td  valign="bottom" align="left" style="font-size:10px;max-width:110px;">X'+orderlist[i].peoplenum+'</tr>';
							        	dataHtml += '</table>';
							       dataHtml += '</div>';
							       dataHtml += '</div>';
							       dataHtml += '</div>';
							       
							       dataHtml += '<div class="am-u-sm-12" style="background: #FFFFFF;text-align: right;border-bottom:1px solid #e5e5e5;padding: 5px 10px">';
							       dataHtml += '共'+orderlist[i].peoplenum+'人 &nbsp;&nbsp;合计:￥'+orderlist[i].total.toFixed(2)+'';
							       dataHtml += '</div>';
							       dataHtml += '<div class="am-u-sm-12" style="background: #FFFFFF;text-align: right;padding:5px 10px">';
							       dataHtml += '<button type="button" onclick="delorder('+i+')" class="am-btn am-btn-warning">删除</button>&nbsp;&nbsp;';
							       
							       if(orderlist[i].status=='待支付'){
							           dataHtml += '<button type="button" class="am-btn am-btn-warning" onclick="pay('+i+')">支付</button>';
							       }
							       
							       dataHtml += '</div>';
							       dataHtml += '';
					           dataHtml += '</div>';
			            
			            }
			            
			            if(type=='物业订单'){
			                  dataHtml += '<div class="am-u-sm-12"  style="margin-top: 5px;padding:0px" id=\'order'+i+'\'>'
						        	dataHtml += '<div class="am-u-sm-12" onclick="orderinfo('+i+')" style="background: #FFFFFF;padding:5px 10px">';
						        	dataHtml += '<span>'+orderlist[i].orderNo+'</span>';
						        	dataHtml += '<span style="float: right;color:#f40">'+orderlist[i].status+'</span>';
						        	dataHtml += '</div>';
							        dataHtml += '<div class="am-u-sm-12" onclick="orderinfo('+i+')" style="background: #FFFFFF;text-align: right;border-bottom:1px solid #e5e5e5;padding: 5px 10px">';
							        dataHtml += '需缴金额:￥'+orderlist[i].total.toFixed(2)+'';
							        dataHtml += '</div>';
							        dataHtml += '<div class="am-u-sm-12" style="background: #FFFFFF;text-align: right;padding:5px 10px">';
							        dataHtml += '<button type="button" onclick="delorder('+i+')" class="am-btn am-btn-warning">删除</button>&nbsp;&nbsp;';
							       
							        if(orderlist[i].status=='待支付'){
							           dataHtml += '<button type="button" class="am-btn am-btn-warning" onclick="pay('+i+')">支付</button>';
							        }
							        
							        dataHtml += '</div>';
							        dataHtml += '';
					           dataHtml += '</div>';
			            
			            }
			            
			            if(type=='其他订单'){
			                 
			            
			            }
			            
			            
			            
			       }
	        
			    }else{
			       
			       dataHtml += '<div  class="am-u-sm-12" style="margin-top:10px;text-align:center">暂无数据...</div>';
			         
			    }
                      
		        $api.html($api.byId('orderlist'), dataHtml);
				
				api.hideProgress();
			    
		       }else{
		            api.alert({
			            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
			        });
			        api.hideProgress();
		       }
	   });
	   
     
     
};


//订单详情
function orderinfo(index){
    
    var order=orderlist[index];
    var data;
    if(type=='商品订单'){
        data={
           'type':'SP',
           '_id':order._id,
           'status':order.status,'receiver':order.receiver,'mobile':order.mobile,
           'address':order.province+order.city+order.district+order.currAddressDetail,
           'proTotal':order.proTotal,'freight':order.freight,'total':order.total,
           'orderNo':order.orderNo,'createTime':order.createTime,'payType':order.payType,
           'payTime':order.payTime,'item':order.item,
        };
    }else if(type=='旅游订单'){
        data={
           'type':'LY',
           '_id':order._id,
           'status':order.status,'receiver':order.name,'mobile':order.mobile,
           'proTotal':order.proTotal,'total':order.total,'proDefImg':order.proDefImg,
           'orderNo':order.orderNo,'createTime':order.createTime,'payType':order.payType,
           'payTime':order.payTime,'crnum':order.crnum,'etnum':order.etnum,
           'crtotal':order.crtotal,'ertatol':order.ertatol,
        };
    
    }else if(type=='物业订单'){
        data={
           'type':'WY',
           '_id':order._id,
           'status':order.status,'receiver':order.name,'address':order.compound,
           'total':order.total,'orderNo':order.orderNo,'createTime':order.createTime,
           'payType':order.payType, 'payTime':order.payTime,'item':order.item
        };
    
    }else if(type=='其他订单'){
        data={
           'type':'OT',
           '_id':order._id,
           'status':order.status,
           'total':order.total,'orderNo':order.orderNo,'createTime':order.createTime,
           'payType':order.payType, 'payTime':order.payTime,
        };
    
    }
    
    openWin('orderinfo',{order:data});
};

//删除订单
function delorder(index){
    
    var tp='SP';
    if(type=='商品订单'){
       tp='SP';
    }else if(type=='旅游订单'){
       tp='LY';
    }else if(type=='物业订单'){
       tp='WY';
    }else if(type=='其他订单'){
       tp='OT';
    }
   
    api.confirm({title: '提示',msg:'确定删除吗?',buttons:['确定','取消']},function(ret,err){
			if(ret.buttonIndex == 1){
				
				$api.remove($api.byId("order"+index));
				
				do_ajax('/app/member/delMyOrder','post',{'orderId':orderlist[index]._id,'ordertype':tp},function(ret,err){
				    api.hideProgress();
				    if(ret.resultCode=='0'){
				        toast('删除成功');
				    }else{
				        toast('操作失败');   
				    }
				
				});
				
				
			}
	});

};

//去支付
function pay(index){
   api.openWin({
	    name: 'pay',
	    url: "../pay.html",
	    pageParam: {orderinfo:orderlist[index]},
	   }
	);

};


//打开订单类型
function openmenu(){
  if(a==0){
      $api.css($api.byId('typemenu'),'display:block');
      a=1;
  }else{
       $api.css($api.byId('typemenu'),'display:none');
       a=0;
  }

};

//收货
function takeback(index){
    var order=orderlist[index];
    
     api.confirm({title: '提示',msg:'确定收到商品吗?',buttons:['确定','取消']},function(ret,err){
		if(ret.buttonIndex == 1){
			do_ajax('/app/member/orderRec','post',{'orderNo':order.orderNo},function(respose,err){
		        api.hideProgress();
		        if(respose.resultCode=='0'){
		           toast('收货成功');
		        }else{
		           toast(respose.datas);
		        }
		        
		        //发送订单改变事件
		        api.sendEvent({
				    name: 'changeOrder',
				});
				
				
			});
		}
	 });
    
   
	
    
    
};


//去评价
function addcomment(index){
    
    openWin('commentpro',{'order':orderlist[index]});
    
};






